//index.js
//获取应用实例
var app = getApp();
var modal = require('../../utils/modal');
var request = require('../../utils/request');
var utils = require('../../utils/utils');
var addrPicker = require('../../components/addrPicker/addrPicker');
var storage = require('../../utils/storage');

var pageSetting = {
  data: {
    medalMaskFlag:false
  },
  onShow: function (options) {
    modal.loading();
    var that = this;
    app.getUserInfo(function (userInfo) {
      that.setData({
        userInfo: userInfo
      });
    });

    /*为组件填充数据*/
    var data = {
      'self:oc:order:ucenter': {}
    }
    request.post({
      path: '/wxapp_api/api'
    }, data)
      .then(function (res) {
        var retData = res['self:oc:order:ucenter'];
        if (retData.ret != 0) {
          // modal.toast(retData.msg);
          wx.navigateTo({
              url: "../error/error"
          })
          return false ;
        }
        var dataLen = retData['data'];
        that.setData({
          orderListLen: dataLen.orderCount,
          messageCount: dataLen.messageCount,
          noticePoints: dataLen.noticePoints,
          publicMedalCount: dataLen.publicMedalCount
        });
        console.log(dataLen);
        modal.hide();
      });
      request.bind();

    // 我的消息
  /*  var noticeData = {
      'self:oc:order:ucenter': {}
    }
    
    request.post({
      path: '/wxapp_api/api'
    }, noticeData)
      .then(function (res) {
        console.log(res) ;
        var retData = res['data']['self:oc:order:ucenter'];
        if (retData.ret != 0) {
          modal.toast(retData.msg);
          return false;
        }
        var dataLen = retData['data'];
        console.log(dataLen) ;
        that.setData({
          noticePoints: dataLen['noticePoints'],
          messageCount:dataLen['messageCount']
        });
        modal.hide();
      });*/
  },

  gonIndex: function () {
    wx.redirectTo({
      url: '../project-index/index'
    })
  },
  tapNotice: function (options) {
    console.log(this.data);
    this.setData({
      noticePoints: false
    })
    return false;
  },
  showMedalMask: function(){
      this.setData({
          medalMaskFlag : true
      });
  },
  hideMedalMask: function(){
      this.setData({
          medalMaskFlag : false
      });
  },
  changePhone:function(){
    var id = wx.getStorageSync('sessionId');
    var data = {
      'self:sms:bindInfo':{
        'wx_session':id
      }
    }
    request.post({
      path: '/wxapp_api/api'
    },data)
      .then(function (res) {
        var retData = res['self:sms:bindInfo'];
        if (retData.ret != 0) {
          wx.navigateTo({
              url: "../error/error"
          })
          return false ;
        }
        var dataLen = retData['data'];
        wx.setStorageSync('need_bind',dataLen.need_bind);
        wx.setStorageSync('address_mobile',dataLen.address_mobile);
        wx.setStorageSync('mobile',dataLen.mobile);
        if (dataLen.need_bind == "1" && dataLen.mobile == "") {
          wx.navigateTo({
            url: "../has-link-refuse/has-link-refuse"
          })
        }else{
          wx.navigateTo({
            url: "../has-link/has-link"
          })
        }
      })

    // var phone = wx.getStorageSync('phoneNum')
    // if (phone == "") {
    //   var url = "../has-link-refuse/has-link-refuse"
    // }else{
    //   var url = "../has-link/has-link"
    // }
    // wx.navigateTo({
    //   url: url
    // })
  }
};

var addrPickerOption = {
  data: {
    wiiAddrPicker: {
      'addrPickerUrl': '/wxapp_api/api',
      'addrPickerKey': {
        'setDefault': 'self:uc_api:address:setDefault',
        'delAddr': 'self:uc_api:address:del',
        'getAddrList': 'self:uc_api:address:get',
        'addAddr': 'self:uc_api:address:add'
      }
    }
  }
};

utils.assign(pageSetting, addrPicker, addrPickerOption);
Page(pageSetting);